package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

/**
 * @author 柳文迪
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 清理缓存
     */
    public void cleanCache(String pattern){
        //获取所有符合条件的键
        Set keys = redisTemplate.keys(pattern);
        //删除清理掉这些键
        redisTemplate.delete(keys);
    }
    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @PostMapping
    @ApiOperation("管理端-新增菜品")
    public Result addDish(@RequestBody DishDTO dish) {
        //打印日志输出
        log.info("dish传来的结果:{}",dish);
        dishService.addDish(dish);
        //清理缓存
        cleanCache("dish_" + dish.getCategoryId());
        return Result.success();
    }


    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result pageSelectDish(DishPageQueryDTO dishPageQueryDTO){
            PageResult pageResult = dishService.pageSelectDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Integer> ids){
        dishService.deleteDish(ids);
        //清理所有缓存
        cleanCache("dish_*");
        log.info("清理所有缓存,菜品可能会被批量删除");
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectByIdDish(@PathVariable Integer id){
        DishVO dishVO = dishService.selectByIdDish(id);
        return Result.success(dishVO);
    }

    /**
     * 分类查询
     * @param categoryId
     * @return
     */

    @GetMapping("/list")
    public Result list(Integer categoryId){
        List<Dish> dish = dishService.list(categoryId);
        return Result.success(dish);
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */

    @PostMapping("/status/{status}")
    public Result updateStatusDish(@PathVariable Integer status ,Integer id){
        log.info("结果是：{},{}",status,id);
        dishService.updateStatusDish(status,id);
        return Result.success();
    }

    /**
     * 修改菜品数据
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("传来的数据是:{}",dishDTO);
        dishService.updateDish(dishDTO);
        //清理缓存
        cleanCache("dish_*");
        log.info("清理所有缓存,菜品有更新");
        return Result.success();
    }
}
